#include <regex>

#include "common/more/uuid.h"
#include "crow_helper.h"

namespace Common
{

Uuids::Uuid currentSessionUuid(const crow::request& request)
{
    const std::string cookieHeader = request.get_header_value("Cookie");
    std::regex pattern("sessionUuid=([0-9a-fA-F-]+)");
    std::smatch match;
    if (!std::regex_search(cookieHeader, match, pattern))
        return Uuids::nilUuid();

    const std::string uuid = match[1];
    return Uuids::stringToUuid(uuid);
}

void setSessionUuid(crow::response& response, const Uuids::Uuid& uuid)
{
    response.add_header(
        "Set-Cookie",
        "sessionUuid=" + Uuids::uuidToString(uuid)
    );
}

crow::response redirect(const std::string& path)
{
    crow::response response;
    response.redirect(path);
    return response;
}

crow::response movedPermanently(const std::string& path)
{
    crow::response response;
    response.moved_perm(path);
    return response;
}

} // namespace Common
